﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Web.Security;
using CCF20Module.Properties;
using System.Web.Profile;
using CCF20Model;
namespace CCF20Module
{
    /// <summary>
    /// Interaction logic for UserCreateWindow.xaml
    /// </summary>
    public partial class UserCreateWindow : Window
    {

        public string SelectedRole { get; set; }
        public UserCreateWindow()
        {
            InitializeComponent();
        }
        

        private void btnCancel_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        private void btnOk_Click(object sender, RoutedEventArgs e)
        {
            MembershipCreateStatus status;
            
            MembershipUser newUser = Membership.CreateUser(
                txtUsername.Text,
                txtPassword.Text,
                txtEmailAddress.Text,
                txtPasswordQuestion.Text,
                txtPasswordAnswer.Text,
                chkIsApproved.IsChecked.Value,
                out status);
            
            ProfileBase profile = ProfileBase.Create(txtUsername.Text, true);
            profile.SetPropertyValue("Mobile", txtMobile.Text);
            profile.Save();
            Utility.ShowMessageBox(status.GetUIDescription());

            if (status == MembershipCreateStatus.Success)
            {

                if (SelectedRole != string.Empty && SelectedRole != "所有分组" && !Roles.IsUserInRole(newUser.UserName, SelectedRole))
                {
                    Roles.AddUserToRole(newUser.UserName, SelectedRole);
                }
                this.Close();
            }
        }

        
    }
}
